<?php
import( 'de.ceus-media.framework.krypton.core.DefinitionView' );
import( 'classes.view.panel.ProjectSelector' );
import( 'classes.logic.Server' );
import( 'classes.logic.ServerProject' );
/**
 *	Option Views.
 *	@extends		Core_View
 *	@uses			View_Panel_PlatformSelector
 *	@author			Christian WÃ¼rker <Christian.Wuerker@CeuS-Media.de>
 *	@since			06.08.2007
 *	@version		0.1
 */
/**
 *	Option Views.
 *	@extends		Core_View
 *	@uses			View_Panel_PlatformSelector
 *	@author			Christian WÃ¼rker <Christian.Wuerker@CeuS-Media.de>
 *	@since			06.08.2007
 *	@version		0.1
 */
class View_Administration_Server extends Framework_Krypton_Core_DefinitionView
{
	private $nodeId;

	/**
	 *	Builds Car Views.
	 *	@access		public
	 *	@return		string
	 */
	public function buildContent()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
//		$projectSelect	= new View_Panel_ProjectSelector();
//		$ui['content']	= $projectSelect->buildContent();
		
		$serverId		= $request->get( 'serverId' );
		
		if( $serverId )
		{
			$ui['content']	= $this->buildContentEdit( $serverId );
		}
		else
		{
			$ui['content']	= $this->buildContentList();
			$ui['content']	.= $this->buildContentAdd();
		}
		return $ui['content'];
		return $this->loadTemplate( 'administration.server.content', $ui );
	}

	private function buildContentAdd()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$words		= $this->words['server']['addServer'];
		
		//  --  UI COMPONENTS  --  //
		$ui	= $this->buildForm( 'administration/server', 'addServer', 'server', 'addServer', array() );
		$button_add		= UI_HTML_Elements::Button( 'addServer', $words['button_add'], 'but add' );
		$ui['field_button']	= UI_HTML_Elements::Field( '', $button_add );

		$ui['form']		= UI_HTML_Elements::Form( 'addServer', "?link=".$request->get( 'link' ) );
		$ui['caption']	= UI_HTML_Elements::TableCaption( $words['caption'], 'add' );
		$ui['colgroup']	= UI_HTML_Elements::ColumnGroup( $words['colgroup'] );
		return $this->loadTemplate( 'administration.server.add', $ui );
	}

	private function buildContentEdit( $serverId )
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$words		= $this->words['server']['editServer'];
		
		$data	= Logic_Server::getServerData( $serverId );

		//  --  UI COMPONENTS  --  //
		$ui	= $this->buildForm( 'administration/server', 'editServer', 'server', 'editServer', $data );
		$button_cancel	= UI_HTML_Elements::LinkButton( '?link='.$request->get( 'link' ), $words['button_cancel'], 'but cancel' );
		$button_edit	= UI_HTML_Elements::Button( 'editServer', $words['button_edit'], 'but edit' );
		$button_remove	= UI_HTML_Elements::LinkButton( "?link=".$request->get( 'link' )."&removeServer=".$serverId, $words['button_remove'], 'buthot remove', $words['button_remove_confirm'] );
		$ui['field_button_cancel']	= UI_HTML_Elements::Field( '', $button_cancel );
		$ui['field_button_edit']	= UI_HTML_Elements::Field( '', $button_edit/*.$button_remove*/ );

		$ui['form']		= UI_HTML_Elements::Form( 'editServer', "?link=".$request->get( 'link' )."&serverId=".$serverId );
		$ui['caption']	= UI_HTML_Elements::TableCaption( $words['caption'], 'edit' );
		$ui['colgroup']	= UI_HTML_Elements::ColumnGroup( $words['colgroup'] );
		return $this->loadTemplate( 'administration.server.edit', $ui );
	}

	private function buildContentList()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$link		= $request->get( 'link' );
		$logic		= new Logic_Server();
		$servers	= $logic->getServerList();
		$list	= array();
		$ui		= $this->words['server']['listServers'];
		foreach( $servers as $server )
		{
			$projects	= Logic_ServerProject::getProjectsFromServer( $server['server_id'], 2 );
			foreach( $projects as $projectId => $projectData )
			{
				$title		= UI_HTML_Elements::Acronym( $projectData['title'], $projectData['description'] );
				$projects[$projectId]	= UI_HTML_Elements::Link( "?link=manageProjects&projectId=".$projectId, $title );
			}
			$server['projects']	= implode( "<br/>", array_values( $projects ) );
			$server['link']		= $link;
			$server['created']	= $server['created'] ? date( "d.m.Y H:i", $server['created'] ) : "";
			$server['modified']	= $server['modified'] ? date( "d.m.Y H:i", $server['modified'] ) : "";
			$list[]	= $this->loadTemplate( 'administration.server.item', $server );
		}
		$ui['list']	= implode( "\n", $list );
		return $this->loadTemplate( 'administration.server.list', $ui );
	}
}
?>